/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AppGUI.java
 *
 * Created on 04/06/2011, 16:56:25
 */

package pmtarefa2.gui;

/**
 *
 * @author fmenna
 */
import java.text.ParseException;
import javax.swing.JOptionPane;
import javax.swing.text.MaskFormatter;
import pmtarefa2.dominio.Solicitante;
import pmtarefa2.regras.RegraException;
import pmtarefa2.regras.RegraLimiteEmprestimo;

public class AppGUI extends javax.swing.JFrame {
    private RegraLimiteEmprestimo regraLimiteEmprestimo;

    /** Creates new form AppGUI */
    public AppGUI(RegraLimiteEmprestimo regra) {
        initComponents();
        regraLimiteEmprestimo=regra;
    }

    public AppGUI() {
        initComponents();
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboSexo = new javax.swing.JComboBox();
        jLblSalario = new javax.swing.JLabel();
        jLblSexo = new javax.swing.JLabel();
        jLblIdade = new javax.swing.JLabel();
        jBtnCalcular = new javax.swing.JButton();
        jLblEmprestimo = new javax.swing.JLabel();
        jTxtSalario = new javax.swing.JFormattedTextField();
        MaskFormatter mask = null;
        try {
            mask = new MaskFormatter("##");
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        jTxtIdade = new javax.swing.JFormattedTextField(mask);
        jTxtEmprestimoMaximo = new javax.swing.JFormattedTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jComboSexo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Masculino", "Feminino" }));
        jComboSexo.setName("jSexo"); // NOI18N

        jLblSalario.setText("Salário");
        jLblSalario.setName("lblSalario"); // NOI18N

        jLblSexo.setText("Sexo");

        jLblIdade.setText("Idade");
        jLblIdade.setName("lblIdade"); // NOI18N

        jBtnCalcular.setText("Calcular");
        jBtnCalcular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCalcularActionPerformed(evt);
            }
        });

        jLblEmprestimo.setText("Empréstimo Máximo");

        jTxtIdade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTxtIdadeActionPerformed(evt);
            }
        });

        jTxtEmprestimoMaximo.setEditable(false);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(34, 34, 34)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jLblSexo)
                        .add(18, 18, 18)
                        .add(jComboSexo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(jLblSalario)
                        .add(18, 18, 18)
                        .add(jTxtSalario, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 138, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(jLblIdade)
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jBtnCalcular)
                            .add(jTxtIdade, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 32, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(103, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(30, Short.MAX_VALUE)
                .add(jLblEmprestimo)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jTxtEmprestimoMaximo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 142, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLblSalario)
                    .add(jTxtSalario, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jComboSexo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLblSexo))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLblIdade)
                    .add(jTxtIdade, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jBtnCalcular)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLblEmprestimo)
                    .add(jTxtEmprestimoMaximo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnCalcularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCalcularActionPerformed
        Solicitante solicitante = new Solicitante(Float.parseFloat(jTxtSalario.getText()), jComboSexo.getSelectedIndex(), Integer.parseInt(jTxtIdade.getText()));
        try {
            jTxtEmprestimoMaximo.setValue(regraLimiteEmprestimo.avaliar(solicitante));
        } catch (RegraException ex) {
            JOptionPane.showMessageDialog(null,ex.getMessage(),"Erro",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jBtnCalcularActionPerformed

    private void jTxtIdadeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTxtIdadeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTxtIdadeActionPerformed

    /**
    * @param args the command line arguments
    */
    /*public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AppGUI().setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnCalcular;
    private javax.swing.JComboBox jComboSexo;
    private javax.swing.JLabel jLblEmprestimo;
    private javax.swing.JLabel jLblIdade;
    private javax.swing.JLabel jLblSalario;
    private javax.swing.JLabel jLblSexo;
    private javax.swing.JFormattedTextField jTxtEmprestimoMaximo;
    private javax.swing.JFormattedTextField jTxtIdade;
    private javax.swing.JFormattedTextField jTxtSalario;
    // End of variables declaration//GEN-END:variables

}
